'use strict';
const Controller = require('egg').Controller;
const { v4: uuidv4 } = require('uuid');
const { responseFormat, responseHandleFormat, tokenDisabled } = require('../public/js/utils');

class UserController extends Controller {
  async login() {
    const { ctx } = this;
    const { username, password } = ctx.request.body;
    let account = await ctx.service.account.getUserWithUsername(username)
    if(!account || account.password!== password) {
      ctx.body = responseHandleFormat(false, '用户名或密码错误')
      return;
    }
    const user = await ctx.service.user.findUserById(account.id);
    const token = await ctx.service.token.createToken(account.id);
    ctx.body = responseFormat(true, { user, token });
  }
}
module.exports = UserController;
